package com.diaohw.module.system.service.impl;

import com.diaohw.module.system.mapper.LoginLogMapper;
import com.diaohw.module.system.obj.entity.LoginLog;
import com.diaohw.module.system.obj.mapstruct.AccountLoginLogMapstruct;
import com.diaohw.module.system.obj.query.LoginLogQuery;
import com.diaohw.module.system.obj.vo.LoginLogVo;
import com.diaohw.module.system.service.AccountLoginLogService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.remote.auth.obj.bo.LoginLogBO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户登录日志 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-07-08
 */
@Service
public class AccountLoginLogImpl implements AccountLoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    @Override
    public Boolean create(LoginLogBO data) {
        LoginLog model = AccountLoginLogMapstruct.INSTANCE.toEntity(data);
        int insert = loginLogMapper.insert(model);
        return insert > 0;
    }

    @Override
    public LoginLog update(String id, LoginLogBO data) {
        LoginLog model = AccountLoginLogMapstruct.INSTANCE.toEntity(data);
        model.setId(id);
        loginLogMapper.insert(model);
        return model;
    }

    @Override
    public Boolean delete(String id) {
        return loginLogMapper.deleteById(id) > 0;
    }

    @Override
    public LoginLogVo detail(String id) {
        return AccountLoginLogMapstruct.INSTANCE.entityToVo(loginLogMapper.selectOneById(id));
    }

    @Override
    public List<LoginLogVo> findByList(LoginLogQuery query) {
        QueryWrapper wrapper = LoginLogMapper.createWrapper(query);
        return AccountLoginLogMapstruct.INSTANCE.entityToVo(loginLogMapper.selectListByQuery(wrapper));
    }

    @Override
    public PageResultVo<LoginLogVo> findByPage(LoginLogQuery query) {
        QueryWrapper wrapper = LoginLogMapper.createWrapper(query);
        Page<LoginLog> page = loginLogMapper.paginate(query.getPageIndex(), query.getPageSize(), wrapper);
        return PageResultVo.create(page.getTotalRow(), AccountLoginLogMapstruct.INSTANCE.entityToVo(page.getRecords()));
    }


}
